package com.ymatou.iapi.godmch.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.bindmchBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.removemchBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.removemchCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*移除绑定关系
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_removemch{
	private static removemchBean bean;
	private static removemchCall call;
	private static mch_relationWapper mchrelationWapper=new mch_relationWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("移除绑定关系");
	}

	@Before
	public void caseUp() {
		bean=new removemchBean();
		call=new removemchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removemch_001() {
		Logger.start(true,"移除绑定关系,验证普通渠道-商户id与解绑商户id存在上下级关系,返回正常，移除下级与解绑商户拉的商品(通过日志表查看有没有通知商品和交易)");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			//代理
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForAgent2(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//渠道商 邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定
			bindmchBean bindmchbean=GodmchManageCallService.InitbindmchBean(userId_agent, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchbean);
			
			bean.setMchId(userId_channl);
			bean.setUnbindMchId(userId_agent);
			bean.setRemark("测试代理商移除");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证返回成功status值");
	        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
	        Logger.verifyEquals(true, call.getSuccess(), "验证返回成功success值");
			mch_relation mchrelation=mchrelationWapper.selectByPrimaryKey(userId_agent, userId_channl).get(0);
			Logger.verifyEquals(-1, mchrelation.getState(), "验证移除后数据库mch_relation 中的state=-1");
			
			//发消息总线
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("remove_agent-"+bean.getMchId()+"-"+bean.getUnbindMchId(),"agent_remove");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			
			Logger.verifyEquals(String.valueOf(bean.getUnbindMchId()), joa.get(0).get("removedUserId").toString(), "验证总线消息removedUserId");
			Logger.verifyEquals(String.valueOf(bean.getMchId()), joa.get(0).get("userId").toString(), "验证总线消息userId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removemch_002() {
		Logger.start(true,"移除绑定关系,验证vip渠道商-商户id与解绑商户id存在上下级关系,返回正常，不移除下级与解绑商户拉的");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			GodmchManageCallService.SetVip(userId_channl);
			//代理
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForAgent2(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//渠道商 邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定
			bindmchBean bindmchbean=GodmchManageCallService.InitbindmchBean(userId_agent, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchbean);
			
			bean.setMchId(userId_channl);
			bean.setUnbindMchId(userId_agent);
			bean.setRemark("测试代理商移除");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证返回成功status值");
	        Logger.verifyEquals("请求成功", call.getMessage(), "验证返回成功Message值");
	        Logger.verifyEquals(true, call.getSuccess(), "验证返回成功success值");
			mch_relation mchrelation=mchrelationWapper.selectByPrimaryKey(userId_agent, userId_channl).get(0);
			Logger.verifyEquals(-1, mchrelation.getState(), "验证移除后数据库mch_relation 中的state=-1");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removemch_003() {
		Logger.start(true,"移除绑定关系,验证商户id与解绑商户id 不存在上下级关系,返回错误");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			GodmchManageCallService.SetVip(userId_channl);
			//代理
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForAgent2(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//渠道商 邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定
			bindmchBean bindmchbean=GodmchManageCallService.InitbindmchBean(userId_agent, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchbean);
			
			bean.setMchId(userId_channl+999);
			bean.setUnbindMchId(userId_agent);
			bean.setRemark("测试代理商移除");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5028", call.getStatusCode1(), "验证返回成功status值");
	        Logger.verifyEquals(true, call.getMessage().contains("绑定关系不存在"), "验证返回成功Message值");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_removemch_004() {
		Logger.start(true,"移除绑定关系,验证商户id为空，返回错误");
		try {
			UserCreateBean usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean);
			usercreateBean=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean);
			//渠道
			createmchBean createBean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createBean);
			GodmchManageCallService.SetVip(userId_channl);
			//代理
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForAgent2(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//渠道商 邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定
			bindmchBean bindmchbean=GodmchManageCallService.InitbindmchBean(userId_agent, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchbean);
			
			//bean.setMchId();
			bean.setUnbindMchId(userId_agent);
			bean.setRemark("测试代理商移除");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5028", call.getStatusCode1(), "验证返回成功status值");
	        Logger.verifyEquals(true, call.getMessage().contains("绑定关系不存在"), "验证返回成功Message值");	
	        
	        
			call.setData(bean);
			call.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}